const HOST = require('../../config.js').host
const P = require('../../utils/getParam.js').getParam

var app = getApp()


Page({

  /**
   * 页面的初始数据
   */
  data: {
    isHaveCoupons: false,
    activeShoppingList:[],
    bannerType:'',
    bannerId:'',
    activity:[],
    products:[],
    activeType:'A',
  },

  /**
   * 生命周期函数--监听页面加载
   */

  // 获取扫码参数
  getPr : (name,s)=>{
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      var r = str.search.substr(1).match(reg);
      if (r != null) return unescape(r[2]); return r
  },
  backToIndex(){
    wx.switchTab({
      url: '../home/home',
    })
  }
  ,
  getBannerActive(bannerId){
    let self = this;
    wx.request({
      url: `${HOST}v1/banner/newBanner/${bannerId}`,
      success:res=>{
        console.log(res)
        if (res.data.sysCode == '1'){
          let products = res.data.data.products
          let imgUrl = ''
          let activity = res.data.data.activity
          console.log(products)
          for (let i = 0; i < products.length; i++) {
            imgUrl = products[i].portalImg.split(',')[0]
            products[i].portalImg = imgUrl
          }
          self.setData({
            activity: activity,
            products: products,
          })
        }
      }
    })
  }
  ,
  goToYouLike(e) {
    wx.navigateTo({
      url: `../detail/detail?id=${e.currentTarget.dataset.productid}`,
    })
  }
  ,
  // 判断是否领取过优惠券
  checkHave(){
      let that = this;
      wx.request({
        url: `${HOST}v1/usercoupons/checkIsHave/${app.userId},${that.data.bannerId}`,
        success:res=>{
          console.log(1,res)
          if(res.data.data == true){
            that.setData({
              activeType: 'B',
            })    
          }else{
            that.setData({
              activeType: 'A',
            }) 
          }
        }
      })

  },
  // 领取活动优惠券
  getActiveTicket(){
    let that = this;
    wx.request({
      url: `${HOST }v1/usercoupons/saveUserCoupons`,
      method:'POST',
      data:{
        userId:app.userId,
        bannerId:that.data.bannerId
      },
      success:res=>{
        console.log(res);
        that.setData({
          activeType: 'B',
        }) 
      }
    })
  },
  onLoad: function (options) {
    let that = this
    var userId = app.userId;
    console.log(options)
    let bannerId = ''
    let bannerName = ''
    let companyId = ''
    let companyKey = ''
    let codeId = ''
    let bannerType = ''
    if (options.q) {
      // let str = decodeURIComponent(options.q)
      // console.log('URL'+ str)

      // bannerId = str.match(/id=(\S*)&bannerName/)[1] || "";
      // bannerName = str.match(/bannerName=(\S*)&companyId/)[1] || "";
      // companyId = str.match(/companyId=(\S*)&companyKey/)[1] || "";
      // companyKey = str.match(/companyKey=(\S*)/)[1 || ""];
      // console.log(bannerId);
      // console.log(bannerName);
      bannerId = P('Id', options.q)
      bannerName = P('bannerName', options.q)
      companyId = P('companyId', options.q)
      companyKey = P('companyKey', options.q)
      bannertype = P('bannerType', options.q)
      console.log('lianjie')

    } else if(options.companyId) {
      console.log("weixing")
      bannerId = options.id
      bannerName = options.bannerName
      bannerType = options.bannerType
      companyId = options.companyId
      companyKey = options.companyKey
    }else{
      bannerId = options.id
      bannerName = options.bannerName
      bannerType = options.bannerType
      console.log('wu')
    }
    if(options.q || options.companyId){
      console.log(companyId,companyKey,'qvdao')
    wx.login({
      success(res) {
        if (res.code) {
          wx.request({
            url: HOST + 'v1/login/' + res.code,
            method: 'GET',
            success(res) {
              if (res.data.sysCode === 1) {  
                let userId = res.data.data 
                wx.request({
                  url: `${HOST}v1/qrcode/findByCompanyIdAndCompanyKey/${companyId}/${companyKey}`,
                  success: res => {
                    console.log('abc',res)
                    codeId = res.data.data.id
                    console.log('codeId:' + codeId)
                    setTimeout(function () {
                      wx.request({
                        url: `${HOST}v1/scanrecord/saveScanRecord`,
                        data: {
                          codeId: codeId,
                          recordType: 'A',
                          userId: app.userId || userId
                        },
                        method: 'POST',
                        success: res => {
                          app.codeId = codeId
                          console.log('abc1',res)
                          console.log(codeId)
                          console.log(app.userId)
                        }
                      }, 200)
                    })
                  }
                })

              }
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    })
    }
    
    // options.q = 'id=4&bannerName=ss'

 
    wx.setNavigationBarTitle({
      title: bannerName,
    })
    that.setData({
      bannerType,
      bannerId 
    })
    console.log(bannerId)
    if (bannerType == '1' || bannerType == '4'){
      this.getActiveShoppingList(bannerId);
    }else{
      this.getBannerActive(bannerId);
    }
    that.checkHave()
  console.log(this.data)
  },

  /**
   * 获取活动商品
   */
  getActiveShoppingList(bannerId){
    // console.log(this.data.isHaveCoupons);
    let self = this;
    wx.request({
      url: HOST + 'v1/activity/getListByBannerId/' + bannerId,
      method: 'GET',
      success: function (res) {
        console.log(res)
        if (res.data.sysCode === 1) {
          self.setData({
            activeShoppingList: res.data.data
          })
        }
      }
    })
  }, 
  goDetail(e) 
  {
    if (e.currentTarget.dataset.id == " ")
    {
      return;
    } else {
      wx.navigateTo({
        url: `../detail/detail?id=${e.currentTarget.dataset.id}`
      })
    } 
    
  }
})